package com.contract.management.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.contract.management.domain.CommJpaRepository;
import com.contract.management.domain.User;
import com.contract.management.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@Autowired
	private UserService service;
	
	@RequestMapping("/password")
	public String index() {
		return "password";
	}
	
	@RequestMapping(value = "/password", method = RequestMethod.POST)
	public String password(HttpSession session, Model model, String oldPassword, String password) {
		User m = (User)session.getAttribute("user");
		int status = service.changePassword(oldPassword, password, m.getId());
		if(status == -1) {
			model.addAttribute("error", "原密码错误");
			return "password";
		}
		model.addAttribute("title", "密码修改成功");
		return "show_message";
	}
}
